// src/store/store.ts
import { configureStore, combineReducers } from '@reduxjs/toolkit';
import {
  persistReducer,
  FLUSH,
  REHYDRATE,
  PAUSE,
  PERSIST,
  PURGE,
  REGISTER,
} from 'redux-persist';
import AsyncStorage from '@react-native-async-storage/async-storage';

// Slices
import authReducer from './slices/authSlice';
import chatReducer from './slices/chatSlice';
import translateReducer from './slices/translateSlice';
import voiceReducer from './slices/voiceSlice';
import subscriptionReducer from './slices/subscriptionSlice';
import settingsReducer from './slices/settingsSlice';
import notificationReducer from './slices/notificationSlice';
import uiReducer from './slices/uiSlice';

// Persist配置
const persistConfig = {
  key: 'root',
  version: 1,
  storage: AsyncStorage,
  whitelist: ['auth', 'settings', 'subscription'], // 需要持久化的reducer
  blacklist: ['ui', 'notification'], // 不需要持久化的reducer
};

const rootReducer = combineReducers({
  auth: authReducer,
  chat: chatReducer,
  translate: translateReducer,
  voice: voiceReducer,
  subscription: subscriptionReducer,
  settings: settingsReducer,
  notification: notificationReducer,
  ui: uiReducer,
});

const persistedReducer = persistReducer(persistConfig, rootReducer);

export const store = configureStore({
  reducer: persistedReducer,
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({
      serializableCheck: {
        ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
        ignoredPaths: ['chat.messages', 'voice.records'],
      },
    }),
});

export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;